package com.ln.adoms.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ln.adoms.service.GoodsService;
import com.ln.core.security.entity.Users;

@Controller
@RequestMapping("/wap/pda")
public class PdaAction extends BaseAction {

	private static final Logger LOGGER = Logger.getLogger(PdaAction.class);

	@Resource
	private GoodsService goodsService;

	@RequestMapping(value = "/in")
	public String pdaIn(Model model, HttpServletRequest request) {
		Integer departmentId = this.getCurrentDepartmentId(request);
		goodsService.commonPda(model, departmentId);
		return "/wap/pda/in";
	}

	@RequestMapping(value = "/inDone")
	public String inDone(Model model, HttpServletRequest request) {
		return "/wap/pda/inDone";
	}

	@RequestMapping(value = "/outDone")
	public String outDone(Model model, HttpServletRequest request) {
		return "/wap/pda/outDone";
	}

	@RequestMapping(value = "/out")
	public String pdaOut(Model model, HttpServletRequest request) {
		Integer departmentId = this.getCurrentDepartmentId(request);
		goodsService.commonOut(model, departmentId);
		return "/wap/pda/out";
	}

	@RequestMapping(value = "/pdaOut")
	public String pdaOut(Model model, HttpServletRequest request, String qrCode, Integer[] orderLineId, Integer orderId) {
		Integer departmentId = this.getCurrentDepartmentId(request);
		try {
			String msg = goodsService.pdaOut(this.getCurrentUser(request), model, request, qrCode, orderLineId, orderId, departmentId);
			if (msg.endsWith("出库完成.")) {
				return "/wap/pda/outDone";
			}
		} catch (Exception e) {
			LOGGER.error(e);
			model.addAttribute("msg", "<span style='color:red;'>数据异常，请返回主页重试！</span>");
		}
		return "/wap/pda/out";
	}

	@RequestMapping(value = "/login")
	@ResponseBody
	public boolean login(HttpServletRequest request) {
		Users user = this.getCurrentUser(request);
		request.getSession().setAttribute("IS_PDA", true);
		if (user != null) {
			return true;
		} else {
			return false;
		}
	}

	@RequestMapping(value = "/pdaStore")
	public String pdaStore(Model model, HttpServletRequest request, String qrCode, Integer batchId) {
		Integer departmentId = this.getCurrentDepartmentId(request);
		// 入库
		String msg = goodsService.inStore(qrCode, batchId, model, departmentId);
		model.addAttribute("msg", msg);
		if (msg.endsWith("录入完成")) {
			return "/wap/pda/inDone";
		}
		return "/wap/pda/in";
	}

	/**
	 * 清除当前出库批次的所有数据
	 * 
	 * @param batchId
	 * @return boolean
	 */
	@RequestMapping(value = "/clearOutData")
	public String clearOutData(Integer orderId, Model model, HttpServletRequest request) {
		Integer departmentId = this.getCurrentDepartmentId(request);
		goodsService.clearOutData(orderId, model, departmentId);
		return "/wap/pda/out";
	}

	/**
	 * 清除当前入库批次的所有数据
	 * 
	 * @param batchId
	 * @return boolean
	 */
	@RequestMapping(value = "/clearStoreData")
	public String clearStoreData(Integer batchId, Model model, HttpServletRequest request) {
		Integer departmentId = this.getCurrentDepartmentId(request);
		goodsService.clearStoreData(batchId, model, departmentId);
		return "/wap/pda/in";
	}

}
